package com.toocms.jinjuhang.ui.mine

import android.app.Application
import android.os.Bundle
import androidx.databinding.ObservableArrayList
import androidx.databinding.ObservableField
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.StringUtils
import com.toocms.jinjuhang.BR
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.config.Urls
import com.toocms.jinjuhang.config.UserRepository
import com.toocms.jinjuhang.data.User
import com.toocms.jinjuhang.data.center.GetParentPhoneData
import com.toocms.jinjuhang.dialog.dedicated_service.DedicatedServiceDialog
import com.toocms.jinjuhang.ui.mine.account_management.AccountManagementFgt
import com.toocms.jinjuhang.ui.mine.apply_cooperation.ApplyCooperationFgt
import com.toocms.jinjuhang.ui.mine.certification.CertificationFgt
import com.toocms.jinjuhang.ui.mine.contact_us.ContactUsFgt
import com.toocms.jinjuhang.ui.mine.download_section.DownloadSectionFgt
import com.toocms.jinjuhang.ui.mine.integral.MyIntegralFgt
import com.toocms.jinjuhang.ui.mine.involved_result.InvolvedResultFgt
import com.toocms.jinjuhang.ui.mine.management_system.ManagementSystemFgt
import com.toocms.jinjuhang.ui.mine.my_favorite.MyFavoriteFgt
import com.toocms.jinjuhang.ui.mine.national_agency.NationalAgencyFgt
import com.toocms.jinjuhang.ui.mine.order.MyOrderFgt
import com.toocms.jinjuhang.ui.mine.order.OrderStatus
import com.toocms.jinjuhang.ui.mine.personal_data.PersonalDataFgt
import com.toocms.jinjuhang.ui.mine.receiver_address.ReceiverAddressFgt
import com.toocms.jinjuhang.ui.mine.setting.SettingFgt
import com.toocms.jinjuhang.widget.view_model.checkLoginStatus
import com.toocms.jinjuhang.widget.view_model.requestData
import com.toocms.tab.base.BaseActivity
import com.toocms.tab.base.BaseModel
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.binding.ItemBinding
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class MineModel(application: Application) : BaseViewModel<BaseModel>(application) {

    val dataObservableField = ObservableField(User())

    val functionItems = ObservableArrayList<MineFunctionItemModel>()
    val functionItemBinding =
        ItemBinding.of<MineFunctionItemModel>(BR.mineFunctionItemModel, R.layout.item_mine_function)

    val personalDataClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (!checkLoginStatus()) return@BindingAction
        startFragment(PersonalDataFgt::class.java)
    })

    val loginClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        checkLoginStatus()
    })

    val balanceClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (!checkLoginStatus()) return@BindingAction
        startFragment(AccountManagementFgt::class.java)
    })

    val integralClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (!checkLoginStatus()) return@BindingAction
        startFragment(MyIntegralFgt::class.java)
    })

    val orderViewMimeClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (!checkLoginStatus()) return@BindingAction
        val bundle = Bundle()
        bundle.putInt(Constants.BundleKey.KEY_ORDER_STATUS, OrderStatus.ALL.code)
        startFragment(MyOrderFgt::class.java, bundle)
    })
    val orderAwaitPaymentClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (!checkLoginStatus()) return@BindingAction
        val bundle = Bundle()
        bundle.putInt(Constants.BundleKey.KEY_ORDER_STATUS, OrderStatus.AWAIT_PAYMENT.code)
        startFragment(MyOrderFgt::class.java, bundle)
    })
    val orderAwaitShipmentsClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (!checkLoginStatus()) return@BindingAction
        val bundle = Bundle()
        bundle.putInt(Constants.BundleKey.KEY_ORDER_STATUS, OrderStatus.AWAIT_SHIPMENTS.code)
        startFragment(MyOrderFgt::class.java, bundle)
    })
    val orderAwaitReceivingClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (!checkLoginStatus()) return@BindingAction
        val bundle = Bundle()
        bundle.putInt(Constants.BundleKey.KEY_ORDER_STATUS, OrderStatus.AWAIT_RECEIVING.code)
        startFragment(MyOrderFgt::class.java, bundle)
    })
    val orderAwaitEvaluateClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        if (!checkLoginStatus()) return@BindingAction
        val bundle = Bundle()
        bundle.putInt(Constants.BundleKey.KEY_ORDER_STATUS, OrderStatus.AWAIT_EVALUATE.code)
        startFragment(MyOrderFgt::class.java, bundle)
    })

    val functionFacilitatingAgency by lazy {
        MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_facilitating_agency)),
            ObservableField(StringUtils.getString(R.string.str_facilitating_agency)),
            isCheckLoginStatus = true,
            openFgt = ManagementSystemFgt::class.java
        )
    }

    init {
        if (UserRepository.isLogin() && "1" != UserRepository.user.level) {
            functionItems += functionFacilitatingAgency
        }
        functionItems += MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_receiver_address)),
            ObservableField(StringUtils.getString(R.string.str_receiver_address)),
            isCheckLoginStatus = true,
            openFgt = ReceiverAddressFgt::class.java
        )
        functionItems += MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_contact_us)),
            ObservableField(StringUtils.getString(R.string.str_contact_us)),
            openFgt = ContactUsFgt::class.java,
            bundle = Bundle().apply {
                putString(
                    Constants.BundleKey.KEY_TITLE,
                    StringUtils.getString(R.string.str_contact_us)
                )
            }
        )
        functionItems += MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_apply_cooperation)),
            ObservableField(StringUtils.getString(R.string.str_apply_cooperation)),
            isCheckLoginStatus = true,
            openFgt = ApplyCooperationFgt::class.java
        )
        functionItems += MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_download_section)),
            ObservableField(StringUtils.getString(R.string.str_download_section)),
            isCheckLoginStatus = true,
            openFgt = DownloadSectionFgt::class.java
        )
        functionItems += MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_dedicated_service)),
            ObservableField(StringUtils.getString(R.string.str_dedicated_service)),
            isCheckLoginStatus = true,
            itemClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
                if (!checkLoginStatus()) return@BindingAction
                requestData(
                    Urls.Center.GET_PARENT_PHONE,
                    mapOf(Pair("member_id", UserRepository.user.member_id)),
                    resultClass = GetParentPhoneData::class.java,
                    result = {
                        if (it.phone.isNullOrEmpty()) return@requestData
                        DedicatedServiceDialog()
                            .apply {
                                phoneCode = it.phone
                                show(
                                    (ActivityUtils.getTopActivity() as BaseActivity).supportFragmentManager,
                                    null
                                )
                            }
                    })
            })
        )
        functionItems += MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_my_favorite)),
            ObservableField(StringUtils.getString(R.string.str_my_favorite)),
            isCheckLoginStatus = true,
            openFgt = MyFavoriteFgt::class.java
        )
        functionItems += MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_national_agency)),
            ObservableField(StringUtils.getString(R.string.str_national_agency)),
            isCheckLoginStatus = true,
            openFgt = NationalAgencyFgt::class.java
        )
        functionItems += MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_involved_result)),
            ObservableField(StringUtils.getString(R.string.str_involved_result)),
            openFgt = InvolvedResultFgt::class.java,
            isCheckLoginStatus = true
        )
        functionItems += MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_setting)),
            ObservableField(StringUtils.getString(R.string.str_setting)),
            openFgt = SettingFgt::class.java
        )
        functionItems += MineFunctionItemModel(
            this,
            ObservableField(ResourceUtils.getDrawable(R.mipmap.icon_mine_certification)),
            ObservableField("${StringUtils.getString(R.string.app_name)}${StringUtils.getString(R.string.str_certification)}"),
            openFgt = CertificationFgt::class.java
        )
    }

    override fun onResume() {
        super.onResume()
        if (UserRepository.isLogin()) {
            val params = mutableMapOf<String, String?>()
            params["member_id"] = UserRepository.user.member_id
            requestData(Urls.Center.MEMBER_INFO,
                params,
                resultClass = User::class.java,
                result = {
                    UserRepository.updateUser(it)
                    dataObservableField.set(it)
                    if (UserRepository.isLogin() && "1" != UserRepository.user.level) {
                        if (!functionItems.contains(functionFacilitatingAgency)) {
                            functionItems.add(0, functionFacilitatingAgency)
                        }
                    } else {
                        if (functionItems.contains(functionFacilitatingAgency)) {
                            functionItems -= functionFacilitatingAgency
                        }
                    }
                },
                error = {

                })
        } else {
            dataObservableField.set(User())
            if (functionItems.contains(functionFacilitatingAgency)) {
                functionItems -= functionFacilitatingAgency
            }
        }
    }
}